package org.jivesoftware.smackx.packet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

public class OfflineMessageRequest extends IQ
{
  private boolean fetch;
  private List items = new ArrayList();
  private boolean purge = false;

  public OfflineMessageRequest()
  {
    this.fetch = false;
  }

  public void addItem(Item paramItem)
  {
    synchronized (this.items)
    {
      this.items.add(paramItem);
      return;
    }
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<offline xmlns=\"http://jabber.org/protocol/offline\">");
    List localList = this.items;
    monitorenter;
    int i = 0;
    try
    {
      if (i >= this.items.size())
      {
        monitorexit;
        if (this.purge)
          localStringBuilder.append("<purge/>");
        if (this.fetch)
          localStringBuilder.append("<fetch/>");
        localStringBuilder.append(getExtensionsXML());
        localStringBuilder.append("</offline>");
        return localStringBuilder.toString();
      }
      localStringBuilder.append(((Item)this.items.get(i)).toXML());
    }
    finally
    {
      monitorexit;
    }
  }

  public Iterator getItems()
  {
    synchronized (this.items)
    {
      Iterator localIterator = Collections.unmodifiableList(new ArrayList(this.items)).iterator();
      return localIterator;
    }
  }

  public boolean isFetch()
  {
    return this.fetch;
  }

  public boolean isPurge()
  {
    return this.purge;
  }

  public void setFetch(boolean paramBoolean)
  {
    this.fetch = paramBoolean;
  }

  public void setPurge(boolean paramBoolean)
  {
    this.purge = paramBoolean;
  }

  public static class Item
  {
    private String action;
    private String jid;
    private String node;

    public Item(String paramString)
    {
      this.node = paramString;
    }

    public String getAction()
    {
      return this.action;
    }

    public String getJid()
    {
      return this.jid;
    }

    public String getNode()
    {
      return this.node;
    }

    public void setAction(String paramString)
    {
      this.action = paramString;
    }

    public void setJid(String paramString)
    {
      this.jid = paramString;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<item");
      if (getAction() != null)
        localStringBuilder.append(" action=\"").append(getAction()).append("\"");
      if (getJid() != null)
        localStringBuilder.append(" jid=\"").append(getJid()).append("\"");
      if (getNode() != null)
        localStringBuilder.append(" node=\"").append(getNode()).append("\"");
      localStringBuilder.append("/>");
      return localStringBuilder.toString();
    }
  }

  public static class Provider
  implements IQProvider
  {
    private OfflineMessageRequest.Item parseItem(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      int i = 0;
      OfflineMessageRequest.Item localItem = new OfflineMessageRequest.Item(paramXmlPullParser.getAttributeValue("", "node"));
      localItem.setAction(paramXmlPullParser.getAttributeValue("", "action"));
      localItem.setJid(paramXmlPullParser.getAttributeValue("", "jid"));
      while (true)
      {
        do
          if (i != 0)
            return localItem;
        while ((paramXmlPullParser.next() != 3) || (!(paramXmlPullParser.getName().equals("item"))));
        i = 1;
      }
    }

    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      OfflineMessageRequest localOfflineMessageRequest = new OfflineMessageRequest();
      int i = 0;
      while (true)
      {
        int j;
        label100: 
        do
          while (true)
          {
            do
              while (true)
              {
                while (true)
                {
                  if (i != 0)
                    return localOfflineMessageRequest;
                  j = paramXmlPullParser.next();
                  if (j != 2)
                    break label100;
                  if (!(paramXmlPullParser.getName().equals("item")))
                    break;
                  localOfflineMessageRequest.addItem(parseItem(paramXmlPullParser));
                }
                if (!(paramXmlPullParser.getName().equals("purge")))
                  break;
                localOfflineMessageRequest.setPurge(true);
              }
            while (!(paramXmlPullParser.getName().equals("fetch")));
            localOfflineMessageRequest.setFetch(true);
          }
        while ((j != 3) || (!(paramXmlPullParser.getName().equals("offline"))));
        i = 1;
      }
    }
  }
}